package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import one.stand.model.CompanyModuleConfigModel;
import one.stand.model.ResultModel;
import one.stand.request.CompanyModuleConfigRequest;
import one.stand.response.CompanyModuleConfigResponse;
import one.stand.service.CompanyModuleConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by renz on 2019/8/27 11:24
 */
@Service
public class DefaultCompanyModuleConfigService extends PublicBaseService implements CompanyModuleConfigService {
    @Override
    public ResultModel<List<CompanyModuleConfigResponse>> findCompanyModuleConfigs(CompanyModuleConfigRequest request) {
        log.info("获取企业的配置信息: {}", JSON.toJSONString(request));
        if (null == request.getCompanyId()) {
            return ResultModel.checkParamFail("请求参数company为空");
        }
        List<CompanyModuleConfigModel> resultList = companyModuleConfigMapper.findAllByCompanyId(request.getCompanyId());
        if (resultList.isEmpty()) {
            return ResultModel.noData();
        }
        List<CompanyModuleConfigResponse> responseList = resultList.stream().map(config -> {
            CompanyModuleConfigResponse response = new CompanyModuleConfigResponse();
            BeanUtils.copyProperties(config, response);
            return response;
        }).collect(Collectors.toList());
        return ResultModel.success(responseList);
    }

    @Override
    public ResultModel<Boolean> updateCompanyModuleConfig(CompanyModuleConfigRequest request) {
        log.info("修改企业模块配置信息：{}", JSON.toJSONString(request));
        if (request.getCompanyId() == null) {
            return ResultModel.fail("修改企业模板配置信息，企业id不能为空!");
        }
        if (StringUtils.isBlank(request.getModuleName())) {
            return ResultModel.fail("修改企业模板配置信息，模块名称不能为空!");
        }
        CompanyModuleConfigModel configModel = companyModuleConfigMapper.findByCompanyIdAndModuleName(request.getCompanyId(), request.getModuleName());

        try {
            CompanyModuleConfigModel nm = new CompanyModuleConfigModel();
            nm.setClose(request.isClose());
            nm.setCompanyId(request.getCompanyId());
            nm.setModuleName(request.getModuleName());
            nm.setId(request.getId());
            if (configModel == null) {
                companyModuleConfigMapper.insert(nm);
                log.info("新增企业{} 模块 {} 配置信息为 {} ", request.getCompanyId(), request.getModuleName(), request.isClose());
            } else {
                nm.setId(configModel.getId());
                companyModuleConfigMapper.update(nm);
                log.info("修改企业{} 模块 {} 配置信息为 {} ", request.getCompanyId(), request.getModuleName(), request.isClose());
            }
        } catch (Exception e) {
            log.error("修改企业模板配置信息失败：{}", JSON.toJSONString(request), e);
            return ResultModel.fail("修改企业模板配置信息失败，请稍后再试");
        }
        return ResultModel.success(true);
    }
}